var vm = new Vue({
    el: '#rrapp',
    data: {
        erpOrder: [],
        erpOrderDetail: {}, //订单详情
        orderItemDetailData: [],//订单详情
        orderLogisticsList: [],//订单详情
        commissionStatusEnum: [],
        productionStatusEnum: [],
        totalSum: {},//金额统计
        q: {
            orderNo: '',
            beginCreatetime: '',
            endCreatetime: '',
            productionStatus: '',
            commissionStatus: ''
        },
        page: {currentPage: 0, pageSizes: [20, 50, 100], pageSize: 20, total: 0},
        dialogErpOrderDetail: false
    },

    created: function () {
        this.loadData();
    },
    methods: {
        /* 查询重置 */
        reset: function () {
            vm.q.orderNo = '';
            vm.q.productionStatus = '';
            vm.q.beginCreatetime = '';
            vm.q.endCreatetime = '';
            vm.q.commissionStatus = '';
            this.loadData();
        },
        query: function () {
            this.page.currentPage = 1;
            this.loadData();
        },
        loadData: function () {
            //debugger;
            var params = this.q;
            params.page = this.page.currentPage;
            params.limit = this.page.pageSize;
            $.ajax({
                type: "POST",
                url: baseURL + "order/orderRoyalty/list",
                contentType: "application/json;charset=UTF-8",
                data: JSON.stringify(params),
                success: function (r) {
                    if (r.code == 0) {
                        vm.erpOrder = r.page.list;
                        vm.page.total = r.page.totalCount;
                        vm.commissionStatusEnum = r.userData.commissionStatusEnum;
                        vm.productionStatusEnum = r.userData.productionStatusEnum;
                        vm.totalSum = r.userData.totalSum;
                        // console.log(vm.totalSum)
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
//
        handleSizeChange: function (val) {
            this.page.pageSize = val;
            this.loadData();
        },
        handleCurrentChange: function (val) {
            this.page.currentPage = val;
            this.loadData();
        },
        /**
         * 批量审核
         * @returns {boolean}
         */
        batchAudit: function () {
            var selected = this.$refs.multipleTable.selection;
            var orderNos = [];
            var _this = this;
            for (var index in selected) {
                orderNos.push(selected[index].orderNo);
            }
            if (orderNos.length == 0) {
                _this.$message.error('最少选择一条数据 !');
                return false;
            }
            this.$confirm('是否确认批量审核, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                callback: function (f) {
                    if (f == 'confirm') {
                        var postData = orderNos.join(',');
                        $.ajax({
                            type: "GET",
                            url: baseURL + "order/orderRoyalty/toBatchAudit?orderNos=" + postData,
                            contentType: "application/json",
                            data: null,
                            success: function (r) {
                                if (r.code === 0) {
                                    _this.$message({
                                        type: 'success',
                                        message: '操作成功!'
                                    }, 200);
                                    setTimeout(function () {
                                        vm.loadData();
                                    }, 1000)
                                } else {
                                    alert(r.msg);
                                }
                            }
                        });
                    }
                }
            })
        },

        //批量录入
        batchInput: function () {
            var selected = this.$refs.multipleTable.selection;
            var orderNos = [];
            var _this = this;
            for (var index in selected) {
                orderNos.push(selected[index].orderNo);
            }
            if (orderNos.length == 0) {
                _this.$message.error('最少选择一条数据 !');
                return false;
            }
            this.$confirm('此操作将批量录入, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function () {
                var postData = orderNos.join(',');
                $.ajax({
                    type: "POST",
                    url: baseURL + "order/orderRoyalty/batchInput?orderNos=" + postData,
                    contentType: "application/json",
                    data: null,
                    success: function (r) {
                        if (r.code == 0) {
                            _this.$message({
                                type: 'success',
                                message: '操作成功!'
                            }, 200);
                            setTimeout(function () {
                                vm.loadData();
                            }, 1000)
                        } else {
                            alert(r.msg)
                        }
                    }
                });
            }).catch(function () {
                _this.$message({
                    type: 'info',
                    message: '已取消操作'
                });
            });
        },
        //审核
        audit: function (orderNo) {
            var _this = this;
            this.$confirm('确认审核该订单, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function () {
                $.ajax({
                    type: "GET",
                    url: baseURL + "order/orderRoyalty/audit?orderNo=" + orderNo,
                    contentType: "application/json",
                    data: null,
                    success: function (r) {
                        if (r.code == 0) {
                            _this.$message({
                                type: 'success',
                                message: '操作成功!'
                            }, 200);
                            setTimeout(function () {
                                vm.loadData();
                            }, 1000)
                        } else {
                            alert(r.msg);
                        }
                    }
                })
            }).catch(function () {
                _this.$message({
                    type: 'info',
                    message: '已取消'
                });
            });
        },
        //撤回
        Revoke: function (orderNo) {
            var _this = this;
            this.$confirm('确认撤销审核, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function () {
                $.ajax({
                    type: "GET",
                    url: baseURL + "order/orderRoyalty/Revoke?orderNo=" + orderNo,
                    contentType: "application/json",
                    data: null,
                    success: function (r) {
                        if (r.code == 0) {
                            _this.$message({
                                type: 'success',
                                message: '操作成功!'
                            }, 200);
                            setTimeout(function () {
                                vm.loadData();
                            }, 1000)
                        } else {
                            alert(r.msg);
                        }
                    }
                })
            }).catch(function () {
                _this.$message({
                    type: 'info',
                    message: '已取消'
                });
            });
        },

        //订单详情
        orderDetail: function (orderNo) {

            vm.dialogErpOrderDetail = true;
            $.get(baseURL + "order/orderRoyalty/info/" + orderNo, function (r) {
                vm.erpOrderDetail = r.erpOrder;
                vm.orderItemDetailData = r.orderItemDetailData;
                vm.orderLogisticsList = r.orderLogisticsList;
                //格式化数据
                var productionStatus = vm.erpOrderDetail.productionStatus;
                switch (productionStatus) {
                    case 1:
                        vm.erpOrderDetail.productionStatus = '待审核';
                        break;
                    case 2:
                        vm.erpOrderDetail.productionStatus = '待分配';
                        break;
                    case 3:
                        vm.erpOrderDetail.productionStatus = '生产中';
                        break;
                    case 4:
                        vm.erpOrderDetail.productionStatus = '生产完成';
                        break;
                    case 5:
                        vm.erpOrderDetail.productionStatus = '已发货';
                        break;
                    case 6:
                        vm.erpOrderDetail.productionStatus = '已完成';
                        break;
                    case 7:
                        vm.erpOrderDetail.productionStatus = '新开单';
                        break;
                }
                var payStatus = vm.erpOrderDetail.payStatus;
                switch (payStatus) {
                    case 0:
                        vm.erpOrderDetail.payStatus = '未支付';
                        break;
                    case 1:
                        vm.erpOrderDetail.payStatus = '已支付';
                        break;
                    case 3:
                        vm.erpOrderDetail.payStatus = '部分支付';
                        break;
                    case 6:
                        vm.erpOrderDetail.payStatus = '待审核';
                        break;
                }
                var shipStatus = vm.erpOrderDetail.shipStatus;
                switch (shipStatus) {
                    case 0:
                        vm.erpOrderDetail.shipStatus = '未发货';
                        break;
                    case 1:
                        vm.erpOrderDetail.shipStatus = '已发货';
                        break;
                    case 2:
                        vm.erpOrderDetail.shipStatus = '部分发货';
                        break;
                }
            });
        }
    }
});